<?php
// +----------------------------------------------------------------------
// | ScrollPHP 日志记录
// +----------------------------------------------------------------------
// | @copyright Copyright (c) 2014 PhpStorm All rights reserved.
// +----------------------------------------------------------------------
// | @license Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | @author LIZHEN
// +----------------------------------------------------------------------
// | @version 0.0.1 (2014-09-04)
// +----------------------------------------------------------------------
// | @info $Id: Logger.class.php 2766 14-9-5 上午9:57 LIZHEN $
// +----------------------------------------------------------------------
// | @package

class Logger extends Base
{
    const LOG_FILE = "php_log_%s.log";
    public static function log($param)
    {
        if (is_object($param)) {
            if (true === C('debug')) {
                throw $param;
            } else {
                $file = sprintf(self::LOG_FILE, strtotime(date("Ymd")));
                file_put_contents(CACHE_PATH . '/' . $file, $param, FILE_APPEND);
            }
        } elseif (is_string($param)) {
            if (true === C('debug')) {
                echo '[INFO] '.$param . "<br/>";
            } else {
                $file = sprintf(self::LOG_FILE, strtotime(date("Ymd")));
                file_put_contents(CACHE_PATH . '/' . $file, $param, FILE_APPEND);
            }
        } else {
        }
    }

    /**
     * 输出debug信息 参数形式  "你好 %s", $name ;
     * @param $text 语句，..
     */
    public static function debug($text)
    {
        $args = func_get_args();
        $debug_log = call_user_func_array('sprintf', $args);
        $debug_log .= "<br />\n";
        if (true === C('debug')) {
            echo '[DEBUG] ' . $debug_log;
        }
    }

    /**
     * 输出错误信息
     * @param $text "你好 %s", $name ;
     */
    public static function error($text)
    {
        $args = func_get_args();
        $debug_log = call_user_func_array('sprintf', $args);
        $debug_log .= "<br />\n";
        if (true === C('debug')) {
            echo '[error] ' . $debug_log;
        } else {
            $file = sprintf(self::LOG_FILE, strtotime(date("Ymd")));
            file_put_contents(CACHE_PATH . '/' . $file, '[ERROR] ' . $debug_log, FILE_APPEND);
        }
    }

    public static function clear()
    {
        $files = glob(CACHE_PATH.'/php_log_*.log');
        foreach($files as $file){
            @unlink($file);
        }
    }
}